# from PyQt5 import QtCore, QtGui, QtWidgets
# from tools.generate_psd import generate_strong_password
# import json
# import tkinter as tk
# import importlib
# global last_user
# import file_config
# from file_config import last_user
# from file_tools import modify_last_user
# from index import MainIndexWindows

# class PasswordGeneratorDialog(QtWidgets.QDialog):
#     def __init__(self, parent=None):
#         super(PasswordGeneratorDialog, self).__init__(parent)
#         self.setWindowTitle("Generate Password")
#         self.setGeometry(400, 200, 300, 250)

#         self.length_label = QtWidgets.QLabel("Length:", self)
#         self.length_label.setGeometry(20, 20, 60, 30)
#         self.length_input = QtWidgets.QSpinBox(self)
#         self.length_input.setGeometry(80, 20, 60, 30)
#         self.length_input.setRange(4, 32)
#         self.length_input.setValue(12)

#         self.uppercase_checkbox = QtWidgets.QCheckBox("Include Uppercase", self)
#         self.uppercase_checkbox.setGeometry(20, 60, 150, 30)
#         self.uppercase_checkbox.setChecked(True)

#         self.lowercase_checkbox = QtWidgets.QCheckBox("Include Lowercase", self)
#         self.lowercase_checkbox.setGeometry(20, 90, 150, 30)
#         self.lowercase_checkbox.setChecked(True)

#         self.digits_checkbox = QtWidgets.QCheckBox("Include Digits", self)
#         self.digits_checkbox.setGeometry(20, 120, 150, 30)
#         self.digits_checkbox.setChecked(True)

#         self.symbols_checkbox = QtWidgets.QCheckBox("Include Symbols", self)
#         self.symbols_checkbox.setGeometry(20, 150, 150, 30)
#         self.symbols_checkbox.setChecked(True)

#         # self.generate_button = QtWidgets.QPushButton("Generate", self)
#         # self.generate_button.setGeometry(100, 200, 80, 30)
#         # self.generate_button.clicked.connect(self.generate_password)
#         self.generate_button = QtWidgets.QPushButton("Generate", self)
#         self.generate_button.setGeometry(100, 200, 80, 30)
#         self.generate_button.clicked.connect(self.show_password_dialog)

#     def generate_password(self):
#         length = self.length_input.value()
#         include_uppercase = self.uppercase_checkbox.isChecked()
#         include_lowercase = self.lowercase_checkbox.isChecked()
#         include_digits = self.digits_checkbox.isChecked()
#         include_symbols = self.symbols_checkbox.isChecked()
#         password = generate_strong_password(length, include_uppercase, include_lowercase, include_digits, include_symbols)
        
#         QtWidgets.QMessageBox.information(self, "Generated Password", f"Your generated password is: {password}")

#         self.parent().password_input.setText(password)
#         self.accept()
#         return password
#     def show_password_dialog(self):
#         password = self.generate_password()

#         # 展示密码
#         password_dialog = PasswordDisplayDialog(password, parent=self)
#         password_dialog.exec_()

# class PasswordDisplayDialog(QtWidgets.QDialog):
#     def __init__(self, password, parent=None):
#         super(PasswordDisplayDialog, self).__init__(parent)
#         self.setWindowTitle("Generated Password")
#         self.setGeometry(500, 300, 300, 150)

#         # 展会生成的密码
#         self.password_label = QtWidgets.QLabel(password, self)
#         self.password_label.setGeometry(20, 20, 260, 30)
#         self.password_label.setAlignment(QtCore.Qt.AlignCenter)

#         # 复制密码
#         self.copy_button = QtWidgets.QPushButton("Copy Password", self)
#         self.copy_button.setGeometry(80, 80, 140, 30)
#         self.copy_button.clicked.connect(self.copy_password)

#     def copy_password(self):
#         clipboard = QtWidgets.QApplication.clipboard()
#         clipboard.setText(self.password_label.text())
#         QtWidgets.QMessageBox.information(self, "Copied", "Password copied to clipboard!")

# class RegisterDialog(QtWidgets.QDialog):
#     def __init__(self, parent=None):
#         super(RegisterDialog, self).__init__(parent)
#         self.setWindowTitle("Register")
#         self.setGeometry(400, 200, 300, 300)

#         self.username_label = QtWidgets.QLabel("Username:", self)
#         self.username_label.setGeometry(20, 30, 80, 30)
#         self.username_input = QtWidgets.QLineEdit(self)
#         self.username_input.setGeometry(100, 30, 170, 30)

#         self.password_label = QtWidgets.QLabel("Password:", self)
#         self.password_label.setGeometry(20, 80, 80, 30)
#         self.password_input = QtWidgets.QLineEdit(self)
#         self.password_input.setGeometry(100, 80, 170, 30)
#         self.password_input.setEchoMode(QtWidgets.QLineEdit.Password)

#         self.confirm_password_label = QtWidgets.QLabel("Confirm Password:", self)
#         self.confirm_password_label.setGeometry(20, 130, 120, 30)
#         self.confirm_password_input = QtWidgets.QLineEdit(self)
#         self.confirm_password_input.setGeometry(140, 130, 130, 30)
#         self.confirm_password_input.setEchoMode(QtWidgets.QLineEdit.Password)

#         self.register_button = QtWidgets.QPushButton("Register", self)
#         self.register_button.setGeometry(40, 180, 80, 30)
#         self.register_button.clicked.connect(self.handle_register)

#         self.generate_button = QtWidgets.QPushButton("Generate Password", self)
#         self.generate_button.setGeometry(150, 180, 120, 30)
#         self.generate_button.clicked.connect(self.show_password_generator)

#     def handle_register(self):
#         print("hhhhhhhhhhhhhhhhhhhhhhhhhh")
#         username = self.username_input.text()
#         password = self.password_input.text()
#         confirm_password = self.confirm_password_input.text()
#         print(username)
#         if password != confirm_password:
#             QtWidgets.QMessageBox.warning(self, "Error", "Passwords do not match")
#         else:
#             if check_existing_account(username, 'accounts.json'):
#                 print("已经存在该账号")
#                 QtWidgets.QMessageBox.warning(self, "Error", "Account already exists.")
#             else:
#                 register_account(username, password, 'accounts.json')
#                 print("写入json文件中")
#                 QtWidgets.QMessageBox.information(self, "Success", "Account registered successfully.")
#                 self.accept()

#     def show_password_generator(self):
#         dialog = PasswordGeneratorDialog(self)
#         dialog.exec_()

#     # def handle_register(self):
#     #     username = self.username_input.text()
#     #     password = self.password_input.text()
#     #     confirm_password = self.confirm_password_input.text()

#     #     if password != confirm_password:
#     #         QtWidgets.QMessageBox.warning(self, "Error", "Passwords do not match")
#     #     else:
#     #         print(f"Registered Username: {username}, Password: {password}")
#     #         self.accept()

#     def show_password_generator(self):
#         dialog = PasswordGeneratorDialog(self)
#         dialog.exec_()

# class LoginDialog(QtWidgets.QDialog):
#     def __init__(self, parent=None):
#         super(LoginDialog, self).__init__(parent)
#         self.setWindowTitle("Login")
#         self.setGeometry(400, 200, 300, 200)

#         self.username_label = QtWidgets.QLabel("Username:", self)
#         self.username_label.setGeometry(20, 30, 80, 30)
#         self.username_input = QtWidgets.QLineEdit(self)
#         self.username_input.setGeometry(100, 30, 170, 30)

#         self.password_label = QtWidgets.QLabel("Password:", self)
#         self.password_label.setGeometry(20, 80, 80, 30)
#         self.password_input = QtWidgets.QLineEdit(self)
#         self.password_input.setGeometry(100, 80, 170, 30)
#         self.password_input.setEchoMode(QtWidgets.QLineEdit.Password)

#         self.login_button = QtWidgets.QPushButton("Login", self)
#         self.login_button.setGeometry(100, 130, 80, 30)
#         self.login_button.clicked.connect(self.handle_login)

#     def handle_login(self):
#         username = self.username_input.text()
#         password = self.password_input.text()
#         print(username,password)
#         accounts = load_accounts('accounts.json')
#         for account in accounts:
#             print(account['account'],username,account['password'],password)
#             if account['account'] == username and account['password'] == password:
#                 print("Login successful")
#                 new_user_index = account['index']
#                 modify_last_user('file_config.py',new_user_index)
#                 self.accept()
#                 return
#             print("登录失败")
#         QtWidgets.QMessageBox.warning(self, "Error", "Invalid username or password.")

# class Ui_LoginWindow(object):
#     def setupUi(self, MainWindow):
#         MainWindow.setObjectName("MainWindow")
#         MainWindow.resize(800, 600)
#         MainWindow.setMouseTracking(True)
#         self.centralwidget = QtWidgets.QWidget(MainWindow)
#         self.centralwidget.setObjectName("centralwidget")
#         self.label = QtWidgets.QLabel(self.centralwidget)
#         self.label.setGeometry(QtCore.QRect(-30, 10, 801, 481))
#         self.label.setTextFormat(QtCore.Qt.AutoText)
#         self.label.setIndent(100)
#         self.label.setObjectName("label")
#         self.pushButton = QtWidgets.QPushButton(self.centralwidget)
#         self.pushButton.setGeometry(QtCore.QRect(150, 510, 161, 51))
#         self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
#         self.pushButton.setMouseTracking(True)
#         self.pushButton.setStyleSheet("background-color: rgb(255, 170, 0);")
#         self.pushButton.setObjectName("pushButton")
#         self.pushButton.clicked.connect(self.show_face_dialog)

#         self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
#         self.pushButton_2.setGeometry(QtCore.QRect(490, 510, 161, 51))
#         self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
#         self.pushButton_2.setMouseTracking(True)
#         self.pushButton_2.setStyleSheet("background-color: rgb(255, 170, 0);")
#         self.pushButton_2.setObjectName("pushButton_2")
#         self.pushButton_2.clicked.connect(self.show_login_dialog)

#         self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
#         self.pushButton_3.setGeometry(QtCore.QRect(320, 510, 161, 51))
#         self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
#         self.pushButton_3.setMouseTracking(True)
#         self.pushButton_3.setStyleSheet("background-color: rgb(255, 170, 0);")
#         self.pushButton_3.setObjectName("pushButton_3")
#         self.pushButton_3.clicked.connect(self.show_register_dialog)

#         self.label_2 = QtWidgets.QLabel(self.centralwidget)
#         self.label_2.setGeometry(QtCore.QRect(580, 360, 221, 51))
#         self.label_2.setObjectName("label_2")
#         MainWindow.setCentralWidget(self.centralwidget)
#         self.statusbar = QtWidgets.QStatusBar(MainWindow)
#         self.statusbar.setObjectName("statusbar")
#         MainWindow.setStatusBar(self.statusbar)

#         self.retranslateUi(MainWindow)
#         QtCore.QMetaObject.connectSlotsByName(MainWindow)

#     def retranslateUi(self, MainWindow):
#         _translate = QtCore.QCoreApplication.translate
#         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#         self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:36pt; font-weight:600;\">The Password Manage Tools</span></p><p align=\"center\"><span style=\" font-size:36pt; font-weight:600;\">Welcome</span></p></body></html>"))
#         self.pushButton.setText(_translate("MainWindow", "人脸识别登录"))
#         self.pushButton_2.setText(_translate("MainWindow", "账号密码登录"))
#         self.pushButton_3.setText(_translate("MainWindow", "注册账号"))
#         self.label_2.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt;\">By Matilda韩泽斌</span></p></body></html>"))

#     def show_login_dialog(self):
#         dialog = LoginDialog()
#         if dialog.exec_() == QtWidgets.QDialog.Accepted:
#             print("Login successful")

#             sys.exit(app.exec_())
#             app = QtWidgets.QApplication(sys.argv)
#             MainWindow = QtWidgets.QMainWindow()
#             ui = MainIndexWindows()
#             ui.show_data_in_line_edit
#             ui.setupUi(MainWindow)
#             MainWindow.show()
#             sys.exit(app.exec_())


#     def show_register_dialog(self):
#         dialog = RegisterDialog()
#         if dialog.exec_() == QtWidgets.QDialog.Accepted:
#             print("Registration successful")

#     def show_face_dialog(self):
#         import face_check
#         importlib.reload(face_check)
#         importlib.reload(file_config)
#         last_user = file_config.last_user
#         print(f"主页面的最终值为{last_user}")


# def check_existing_account(username, filename):
#     print("调用了核验函数")
#     try:
#         with open(filename, 'r') as file:
#             accounts = json.load(file)
#             print(accounts)
#     except FileNotFoundError:
#         accounts = []

#     for account in accounts:
#         if account['account'] == username:
#             print(account['User'])
#             return True
#     return False

# def register_account(username, password, filename):
#     print("调用了注册添加函数")
#     try:
#         with open(filename, 'r') as file:
#             accounts = json.load(file)
#             print(accounts)
#             print("找到文件")
#     except FileNotFoundError:
#         print("文件错误")
#         accounts = []

#     user_nums = len(accounts)
#     print(f"总数为{user_nums}")
#     new_account = {'index': user_nums + 1, 'User': "user"+f"{user_nums+1}", 'account': username, 'password': password}
#     accounts.append(new_account)
#     with open(filename, 'w') as file:
#         json.dump(accounts, file)

# def load_accounts(filename):
#     print("调用了加载函数")
#     try:
#         with open(filename, 'r') as file:
#             accounts = json.load(file)
#     except FileNotFoundError:
#         accounts = []
#     print(accounts)
#     return accounts


# if __name__ == "__main__":
#     import sys
#     app = QtWidgets.QApplication(sys.argv)
#     MainWindow = QtWidgets.QMainWindow()
#     ui = Ui_LoginWindow()
#     ui.setupUi(MainWindow)
#     MainWindow.show()
#     sys.exit(app.exec_())








from PyQt5 import QtCore, QtGui, QtWidgets
from tools.generate_psd import generate_strong_password
import json
import importlib
global last_user
import file_config
from file_config import last_user,psd_key
from file_tools import modify_last_user
from tools.des_enc import des3_encrypt,des3_decrypt


class PasswordGeneratorDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(PasswordGeneratorDialog, self).__init__(parent)
        self.setWindowTitle("Generate Password")
        self.setGeometry(400, 200, 300, 250)

        self.length_label = QtWidgets.QLabel("Length:", self)
        self.length_label.setGeometry(20, 20, 60, 30)
        self.length_input = QtWidgets.QSpinBox(self)
        self.length_input.setGeometry(80, 20, 60, 30)
        self.length_input.setRange(4, 32)
        self.length_input.setValue(12)

        self.uppercase_checkbox = QtWidgets.QCheckBox("Include Uppercase", self)
        self.uppercase_checkbox.setGeometry(20, 60, 150, 30)
        self.uppercase_checkbox.setChecked(True)

        self.lowercase_checkbox = QtWidgets.QCheckBox("Include Lowercase", self)
        self.lowercase_checkbox.setGeometry(20, 90, 150, 30)
        self.lowercase_checkbox.setChecked(True)

        self.digits_checkbox = QtWidgets.QCheckBox("Include Digits", self)
        self.digits_checkbox.setGeometry(20, 120, 150, 30)
        self.digits_checkbox.setChecked(True)

        self.symbols_checkbox = QtWidgets.QCheckBox("Include Symbols", self)
        self.symbols_checkbox.setGeometry(20, 150, 150, 30)
        self.symbols_checkbox.setChecked(True)

        # self.generate_button = QtWidgets.QPushButton("Generate", self)
        # self.generate_button.setGeometry(100, 200, 80, 30)
        # self.generate_button.clicked.connect(self.generate_password)
        self.generate_button = QtWidgets.QPushButton("Generate", self)
        self.generate_button.setGeometry(100, 200, 80, 30)
        self.generate_button.clicked.connect(self.show_password_dialog)

    def generate_password(self):
        length = self.length_input.value()
        include_uppercase = self.uppercase_checkbox.isChecked()
        include_lowercase = self.lowercase_checkbox.isChecked()
        include_digits = self.digits_checkbox.isChecked()
        include_symbols = self.symbols_checkbox.isChecked()
        password = generate_strong_password(length, include_uppercase, include_lowercase, include_digits, include_symbols)
        
        QtWidgets.QMessageBox.information(self, "Generated Password", f"Your generated password is: {password}")

        self.parent().password_input.setText(password)
        self.accept()
        return password
    def show_password_dialog(self):
        password = self.generate_password()

        # 展示密码
        password_dialog = PasswordDisplayDialog(password, parent=self)
        password_dialog.exec_()

class PasswordDisplayDialog(QtWidgets.QDialog):
    def __init__(self, password, parent=None):
        super(PasswordDisplayDialog, self).__init__(parent)
        self.setWindowTitle("Generated Password")
        self.setGeometry(500, 300, 300, 150)

        # 展会生成的密码
        self.password_label = QtWidgets.QLabel(password, self)
        self.password_label.setGeometry(20, 20, 260, 30)
        self.password_label.setAlignment(QtCore.Qt.AlignCenter)

        # 复制密码
        self.copy_button = QtWidgets.QPushButton("Copy Password", self)
        self.copy_button.setGeometry(80, 80, 140, 30)
        self.copy_button.clicked.connect(self.copy_password)

    def copy_password(self):
        clipboard = QtWidgets.QApplication.clipboard()
        clipboard.setText(self.password_label.text())
        QtWidgets.QMessageBox.information(self, "Copied", "Password copied to clipboard!")

class RegisterDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(RegisterDialog, self).__init__(parent)
        self.setWindowTitle("Register")
        self.setGeometry(400, 200, 300, 300)

        self.username_label = QtWidgets.QLabel("Username:", self)
        self.username_label.setGeometry(20, 30, 80, 30)
        self.username_input = QtWidgets.QLineEdit(self)
        self.username_input.setGeometry(100, 30, 170, 30)

        self.password_label = QtWidgets.QLabel("Password:", self)
        self.password_label.setGeometry(20, 80, 80, 30)
        self.password_input = QtWidgets.QLineEdit(self)
        self.password_input.setGeometry(100, 80, 170, 30)
        self.password_input.setEchoMode(QtWidgets.QLineEdit.Password)

        self.confirm_password_label = QtWidgets.QLabel("Confirm Password:", self)
        self.confirm_password_label.setGeometry(20, 130, 120, 30)
        self.confirm_password_input = QtWidgets.QLineEdit(self)
        self.confirm_password_input.setGeometry(140, 130, 130, 30)
        self.confirm_password_input.setEchoMode(QtWidgets.QLineEdit.Password)

        self.register_button = QtWidgets.QPushButton("Register", self)
        self.register_button.setGeometry(40, 180, 80, 30)
        self.register_button.clicked.connect(self.handle_register)

        self.generate_button = QtWidgets.QPushButton("Generate Password", self)
        self.generate_button.setGeometry(150, 180, 120, 30)
        self.generate_button.clicked.connect(self.show_password_generator)

    def handle_register(self):
        print("hhhhhhhhhhhhhhhhhhhhhhhhhh")
        username = self.username_input.text()
        password = self.password_input.text()
        confirm_password = self.confirm_password_input.text()
        print(username)
        if password != confirm_password:
            QtWidgets.QMessageBox.warning(self, "Error", "Passwords do not match")
        else:
            if check_existing_account(username, 'accounts.json'):
                print("已经存在该账号")
                QtWidgets.QMessageBox.warning(self, "Error", "Account already exists.")
            else:
                register_account(username, password, 'accounts.json')
                print("写入json文件中")
                QtWidgets.QMessageBox.information(self, "Success", "Account registered successfully.")
                self.accept()

    def show_password_generator(self):
        dialog = PasswordGeneratorDialog(self)
        dialog.exec_()

    # def handle_register(self):
    #     username = self.username_input.text()
    #     password = self.password_input.text()
    #     confirm_password = self.confirm_password_input.text()

    #     if password != confirm_password:
    #         QtWidgets.QMessageBox.warning(self, "Error", "Passwords do not match")
    #     else:
    #         print(f"Registered Username: {username}, Password: {password}")
    #         self.accept()

    def show_password_generator(self):
        dialog = PasswordGeneratorDialog(self)
        dialog.exec_()
class LoginDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(LoginDialog, self).__init__(parent)
        self.setWindowTitle("Login")
        self.setGeometry(400, 200, 300, 200)

        self.username_label = QtWidgets.QLabel("Username:", self)
        self.username_label.setGeometry(20, 30, 80, 30)
        self.username_input = QtWidgets.QLineEdit(self)
        self.username_input.setGeometry(100, 30, 170, 30)

        self.password_label = QtWidgets.QLabel("Password:", self)
        self.password_label.setGeometry(20, 80, 80, 30)
        self.password_input = QtWidgets.QLineEdit(self)
        self.password_input.setGeometry(100, 80, 170, 30)
        self.password_input.setEchoMode(QtWidgets.QLineEdit.Password)

        self.login_button = QtWidgets.QPushButton("Login", self)
        self.login_button.setGeometry(100, 130, 80, 30)
        self.login_button.clicked.connect(self.handle_login)

    def handle_login(self):
        username = self.username_input.text()
        password = self.password_input.text()
        print(username,password)
        accounts = load_accounts('accounts.json')
        for account in accounts:
            print(account['account'],username,account['password'],password)
            decrypt_psd = des3_decrypt(account['password'],psd_key)
            if account['account'] == username and decrypt_psd == password:
                print("Login successful")
                new_user_index = account['index']
                modify_last_user('file_config.py',new_user_index)
                self.accept()
                # print("即将开启mainwindows")
                # # Transition to the main page
                # self.main_window = QtWidgets.QMainWindow()
                # self.main_ui = MainIndexWindows()
                # self.main_ui.setupUi(self.main_window)
                # self.main_window.show()
                # print("即将关闭mainwindows")
                return

        QtWidgets.QMessageBox.warning(self, "Error", "Invalid username or password.")

class Ui_LoginWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setMouseTracking(True)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(-30, 10, 801, 481))
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setIndent(100)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(150, 510, 161, 51))
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.pushButton.setMouseTracking(True)
        self.pushButton.setStyleSheet("background-color: rgb(255, 170, 0);")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.show_face_dialog)

        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(490, 510, 161, 51))
        self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.pushButton_2.setMouseTracking(True)
        self.pushButton_2.setStyleSheet("background-color: rgb(255, 170, 0);")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.show_login_dialog)

        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(320, 510, 161, 51))
        self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.pushButton_3.setMouseTracking(True)
        self.pushButton_3.setStyleSheet("background-color: rgb(255, 170, 0);")
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.clicked.connect(self.show_register_dialog)

        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(580, 360, 221, 51))
        self.label_2.setObjectName("label_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:36pt; font-weight:600;\">The Password Manage Tools</span></p><p align=\"center\"><span style=\" font-size:36pt; font-weight:600;\">Welcome</span></p></body></html>"))
        self.pushButton.setText(_translate("MainWindow", "人脸识别登录"))
        self.pushButton_2.setText(_translate("MainWindow", "账号密码登录"))
        self.pushButton_3.setText(_translate("MainWindow", "注册账号"))
        self.label_2.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt;\">By Matilda韩泽斌</span></p></body></html>"))

    def show_login_dialog(self):
        dialog = LoginDialog()
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            print("Login successful")
            print("即将开启mainwindows")
            # Transition to the main page
            from index import MainIndexWindows
            self.main_window = QtWidgets.QMainWindow()
            self.main_ui = MainIndexWindows()
            self.main_ui.setupUi(self.main_window)
            self.main_window.show()
            print("即将关闭mainwindows")

    def show_register_dialog(self):
        dialog = RegisterDialog()
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            print("Registration successful")

    def show_face_dialog(self):
        import face_check
        importlib.reload(face_check)
        importlib.reload(file_config)
        last_user = file_config.last_user
        print(f"主页面的最终值为{last_user}")
        from index import MainIndexWindows
        self.main_window = QtWidgets.QMainWindow()
        self.main_ui = MainIndexWindows()
        self.main_ui.setupUi(self.main_window)
        self.main_window.show()

def check_existing_account(username, filename):
    print("调用了核验函数")
    try:
        with open(filename, 'r') as file:
            accounts = json.load(file)
            print(accounts)
    except FileNotFoundError:
        accounts = []

    for account in accounts:
        if account['account'] == username:
            print(account['User'])
            return True
    return False

def register_account(username, password, filename):
    print("调用了注册添加函数")
    try:
        with open(filename, 'r') as file:
            accounts = json.load(file)
            print(accounts)
            print("找到文件")
    except FileNotFoundError:
        print("文件错误")
        accounts = []

    user_nums = len(accounts)
    print(f"总数为{user_nums}")

    password = des3_encrypt(password,psd_key)
    print(f'加密之后的密码为{password}')
    new_account = {'index': user_nums + 1, 'User': "user"+f"{user_nums+1}", 'account': username, 'password': password}
    accounts.append(new_account)
    with open(filename, 'w') as file:
        json.dump(accounts, file)

def load_accounts(filename):
    print("调用了加载函数")
    try:
        with open(filename, 'r') as file:
            accounts = json.load(file)
    except FileNotFoundError:
        accounts = []
    print(accounts)
    return accounts


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_LoginWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())